package net.suncapper.wcss.web.controller;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import jakarta.servlet.http.HttpServletRequest;
import net.suncapper.wcss.common.utls.SessionUtils;
import net.suncapper.wcss.model.dto.DeleteDTO;
import net.suncapper.wcss.model.dto.PageDTO;
import net.suncapper.wcss.model.dto.WhitelistSettingDTO;
import net.suncapper.wcss.model.vo.WhitelistSettingVO;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import net.suncapper.wcss.common.JsonResponse;
import net.suncapper.wcss.service.IWhitelistSettingService;
import net.suncapper.wcss.model.domain.WhitelistSetting;
import org.springframework.web.bind.support.SessionStatus;

import java.io.Writer;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 *
 *  前端控制器
 *
 *
 * @author ztr
 * @since 2025-10-30
 * @version v1.0
 */
@RestController
@RequestMapping("/api/whitelistSetting")
public class WhitelistSettingController {

    private final Logger logger = LoggerFactory.getLogger( WhitelistSettingController.class );

    @Autowired
    private IWhitelistSettingService whitelistSettingService;



    @PostMapping("/login")
    public JsonResponse<WhitelistSetting> login(@RequestBody  WhitelistSetting whitelistSetting, SessionStatus sessionStatus)throws Exception {
        return JsonResponse.success(whitelistSettingService.login(whitelistSetting));
    }
    @GetMapping("/list")
    public JsonResponse List(WhitelistSettingDTO dto, PageDTO pageDTO) {
        Page<WhitelistSettingVO> page=whitelistSettingService.pageList(dto,pageDTO);
        System.out.println(JsonResponse.success(page));
        return JsonResponse.success(page);
    }
    @GetMapping("getCurUserInfo")
    public JsonResponse getCurUserInfo() {
        return JsonResponse.success(SessionUtils.getCurrentLoginUser());
    }
    @GetMapping("clearCurUserInfo")
    public JsonResponse clear()
    {
        return JsonResponse.success(SessionUtils.clearLoginUser());
    }
    @PostMapping("update")
    public  JsonResponse update(@RequestBody WhitelistSetting whitelistSetting)
    {
        boolean b=whitelistSettingService.updateById(whitelistSetting);
        return JsonResponse.success(b);
    }
    @PostMapping("delete")
    public  JsonResponse deleted(@RequestBody WhitelistSetting whitelistSetting)
    {
        boolean b=whitelistSettingService.removeById(whitelistSetting.getId());
        return JsonResponse.success(b);
    }
    @PostMapping("deletes")
    public  JsonResponse deleteds(@RequestBody DeleteDTO deleteDTO)
    {
        boolean b=whitelistSettingService.removeByIds(deleteDTO.getIds());
        return JsonResponse.success(b);
    }
    @GetMapping("/add")
    public JsonResponse addUser(String key)
    {
        List<WhitelistSettingVO> list=whitelistSettingService.getBykey(key);
        return JsonResponse.success(list);
    }
    @PostMapping("adds")
    public JsonResponse addInsert(@RequestBody WhitelistSettingVO  whitelistSettingVO)
    {
        List<WhitelistSetting> users=whitelistSettingService.saveUser(whitelistSettingVO);
        if(users != null && !users.isEmpty() ){
            return JsonResponse.success(2001,"该用户已存在！");
        }
        Integer user=whitelistSettingService.insert(whitelistSettingVO);
        return JsonResponse.success(user);
    }
}

